import os
import sys
sys.path.append('../backend')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'HuaXianGuan.settings')

import django
django.setup()

from apps.flowers.models import Flower, FlowerImage

def check_and_update_images():
    """检查并更新花卉图片URL"""
    
    flowers = Flower.objects.all()
    print(f"检查 {flowers.count()} 个花卉的图片...")
    
    for flower in flowers:
        images = flower.images.all()
        if images.exists():
            for img in images:
                print(f"{flower.name}: {img.image_url} (主图: {img.is_main})")
        else:
            print(f"{flower.name}: 无图片")
    
    # 更新粉郁金香花束的图片URL（如果还没有更新）
    tulip_flower = Flower.objects.filter(name='粉郁金香花束').first()
    if tulip_flower:
        tulip_image = tulip_flower.images.first()
        if tulip_image and 'oss-cn-shanghai.aliyuncs.com' not in tulip_image.image_url:
            tulip_image.image_url = 'https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/粉郁金香花束_1.jpg'
            tulip_image.is_main = True
            tulip_image.save()
            print(f"✓ 已更新粉郁金香花束图片: {tulip_image.image_url}")

if __name__ == "__main__":
    check_and_update_images()